//
//  LayerProgressViewController.swift
//  LayerPrac
//
//  Created by flowerflower on 2019/9/2.
//  Copyright © 2019 cba023. All rights reserved.
//

import UIKit

class LayerProgressViewController: UIViewController {

    let slider = UISlider(frame: .zero)
    
    let layer1 = Progress1Layer()
    let layer2 = Progress2Layer()
    let layer3 = Progress3Layer()
    let layer4 = Progress4Layer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "CALayer图形控制"
        self.view.backgroundColor = .white
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        self.view.addSubview(slider)
        slider.addTarget(self, action: #selector(sliderValueChanged), for: .valueChanged)
        self.view.layer.addSublayer(layer1)
        self.view.layer.addSublayer(layer2)
        self.view.layer.addSublayer(layer3)
        self.view.layer.addSublayer(layer4)
        self.layer1.number = 0.0
        self.layer2.number = 0.0
        self.layer3.number = 0.0
        self.layer4.number = 0.0
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        self.slider.frame = CGRect(x: 50, y: 70, width: self.view.frame.width - 100, height: 30)
        
        let lWH : CGFloat = 100
        let horSpace = (self.view.frame.width - 2 * lWH) / 3.0
        
        layer1.frame = CGRect(x: horSpace, y: 110, width: lWH, height: lWH)
        layer2.frame = CGRect(x: horSpace * 2 + lWH, y: 110, width: lWH, height: lWH)
        layer3.frame = CGRect(x: horSpace, y: 110 + lWH + 30, width: lWH, height: lWH)
        layer4.frame = CGRect(x: horSpace * 2 + lWH, y: 110 + lWH + 30, width: lWH, height: lWH)
    }
    
    @objc func sliderValueChanged(_ sender: UISlider) {
        self.layer1.number = Double(self.slider.value)
        self.layer2.number = Double(self.slider.value)
        self.layer3.number = Double(self.slider.value)
        self.layer4.number = Double(self.slider.value)
    }
}

/// 原型进度条的父类，用于显示百分比文本
class ProgressLayer: CALayer {
    
    var number: Double = 0.0 {
        didSet {
            self.tlayer.string = String(format: "%.0lf%%", self.number * 100)
            self.tlayer.setNeedsDisplay()
            self.setNeedsDisplay()
        }
    }
    
    let tlayer:CATextLayer = {
        let l = CATextLayer()
        let font = UIFont.systemFont(ofSize: 12)
        l.fontSize = font.pointSize
        l.string = ""
        l.isWrapped = true
        l.alignmentMode = CATextLayerAlignmentMode.center
        l.foregroundColor = UIColor.black.cgColor
        return l
    }()
    
    override init() {
        super.init()
        self.addSublayer(tlayer)
        
    }
    
    override init(layer: Any) {
        super.init(layer: layer)
        self.addSublayer(tlayer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSublayers() {
        super.layoutSublayers()
        let tH = NSString(string: "100%").boundingRect(with: CGSize(width: CGFloat.infinity, height: CGFloat.infinity), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 12)], context: nil).height
        self.tlayer.frame = CGRect(x: 0, y: self.frame.height * 0.5 - tH * 0.5, width: self.frame.width, height: tH)
    }
    
}

class Progress1Layer: ProgressLayer {
    
    override func draw(in ctx: CGContext) {
        let radius = self.frame.width * 0.45
        let center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height * 0.5)
        // 线颜色
        ctx.setStrokeColor(UIColor.cyan.cgColor)
        ctx.setLineWidth(radius * 0.08)
        // 设置画笔断点形状
        ctx.setLineCap(CGLineCap.round)
        let endAngle = CGFloat(self.number) * CGFloat.pi * 2.0  - CGFloat.pi * 0.5
        ctx.addArc(center: center, radius: radius, startAngle: -0.5 * CGFloat.pi, endAngle: endAngle, clockwise: false)
        ctx.strokePath()
    }
    
}

class Progress2Layer: ProgressLayer {
    
    override func draw(in ctx: CGContext) {
        let radius = self.frame.width * 0.45
        let center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height * 0.5)
        
        // 圆心
        ctx.move(to: center)
        ctx.addLine(to: CGPoint(x: center.x, y: self.frame.height * 0.05))
         let endAngle = CGFloat(self.number) * CGFloat.pi * 2.0  - CGFloat.pi * 0.5
        ctx.addArc(center: center, radius: radius, startAngle: -0.5 * CGFloat.pi, endAngle: endAngle, clockwise: false)
        ctx.setFillColor(UIColor.yellow.cgColor)
        ctx.fillPath()
    }
    
}

class Progress3Layer: ProgressLayer {
    
    override func draw(in ctx: CGContext) {
        let radius = self.frame.width * 0.45
        let center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height * 0.5)
    
        // 画一个圆圈
        ctx.setStrokeColor(UIColor.gray.withAlphaComponent(0.3).cgColor)
        ctx.setLineWidth(radius * 0.06)
        ctx.addEllipse(in: CGRect(x: self.frame.width * 0.05, y: self.frame.height * 0.05, width: self.frame.width * 0.9, height: self.frame.height * 0.9))
        ctx.strokePath()
        
        let startAngle = CGFloat.pi * 0.5 - CGFloat(self.number) * CGFloat.pi
        let endAngle = CGFloat.pi * 0.5 + CGFloat(self.number) * CGFloat.pi
        ctx.addArc(center: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: false)
        ctx.setFillColor(UIColor.orange.cgColor)
        ctx.fillPath()
    }
    
}

class Progress4Layer: ProgressLayer {
    
    override func draw(in ctx: CGContext) {
        let radius = self.frame.width * 0.45
        let center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height * 0.5)
        
        // 画一个圆圈
        ctx.setStrokeColor(UIColor.gray.withAlphaComponent(0.8).cgColor)
        ctx.setLineWidth(radius * 0.07)
        ctx.addEllipse(in: CGRect(x: self.frame.width * 0.05, y: self.frame.height * 0.05, width: self.frame.width * 0.9, height: self.frame.height * 0.9))
        ctx.strokePath()
        
        // 线颜色
        ctx.setStrokeColor(UIColor.red.cgColor)
        ctx.setLineWidth(radius * 0.08)
        // 设置画笔断点形状
        ctx.setLineCap(CGLineCap.round)
        let endAngle = CGFloat(self.number) * CGFloat.pi * 2.0  - CGFloat.pi * 0.5
        ctx.addArc(center: center, radius: radius, startAngle: -0.5 * CGFloat.pi, endAngle: endAngle, clockwise: false)
        ctx.strokePath()
    }
    
}
